package com.ebt.m.utils.fileutils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.SecretKeySpec;

public class FileDES
{
	/** 加密解密的key */
	private Key mKey;
	/** 解密的密码 */
	private Cipher mDecryptCipher;
	/** 加密的密码 */
	private Cipher mEncryptCipher;

	public FileDES(String key) throws Exception
	{
		initKey(key + "com_ebt_m");
		initCipher();
	}

	/**
	 * 创建一个加密解密的key
	 * 
	 * @param keyRule
	 */
	public void initKey(String keyRule)
	{
		byte[] keyByte = keyRule.getBytes();
		// 创建一个空的八位数组,默认情况下为0
		byte[] byteTemp = new byte[8];
		// 将用户指定的规则转换成八位数组
		for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
			byteTemp[i] = keyByte[i];
		}
		mKey = new SecretKeySpec(byteTemp, "DES");
	}

	/***
	 * 初始化加载密码
	 * 
	 * @throws Exception
	 */
	private void initCipher() throws Exception
	{
		mEncryptCipher = Cipher.getInstance("DES");
		mEncryptCipher.init(Cipher.ENCRYPT_MODE, mKey);

		mDecryptCipher = Cipher.getInstance("DES");
		mDecryptCipher.init(Cipher.DECRYPT_MODE, mKey);
	}

	/**
	 * 加密文件
	 * 
	 * @param in
	 * @param outFile
	 *            加密后保存的位置
	 * @throws IOException
	 */
	public void doEncryptFile(InputStream in, File outFile) throws IOException
	{
		CipherInputStream cin = new CipherInputStream(in, mEncryptCipher);
		try {
			OutputStream os = new FileOutputStream(outFile);
			byte[] bytes = new byte[1024];
			int len;
			while ((len = cin.read(bytes)) > 0) {
				os.write(bytes, 0, len);
				os.flush();
			}
			os.close();
			cin.close();
			in.close();
		} catch (FileNotFoundException e) {
			throw e;
		}
	}

	/**
	 * 加密文件
	 *
	 * @param inputFile
	 *            需要加密的文件
	 * @param outputFile
	 *            加密后保存的位置
	 * @throws IOException
	 */
	public void doEncryptFile(File inputFile, File outputFile) throws IOException {
		doEncryptFile(new FileInputStream(inputFile), outputFile);
	}

	/**
	 * 解密文件
	 * 
	 * @param inputStream
	 * @throws IOException
	 */
	public String doDecryptFile(InputStream inputStream) throws IOException
	{
		StringBuilder builder = new StringBuilder();
		if (inputStream == null) {
			return null;
		}
		try {
			CipherInputStream cin = new CipherInputStream(inputStream, mDecryptCipher);
			BufferedReader reader = new BufferedReader(new InputStreamReader(cin));
			String line;
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			reader.close();
			cin.close();
			inputStream.close();
		} catch (IOException e) {
			throw e;
		}

		return builder.toString();
	}

	/**
	 * 解密文件，传入File类型的参数
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public String doDecryptFile(File file) throws IOException {
		return doDecryptFile(new FileInputStream(file));
	}
}
